// pages/orderDetails/orderDetails.js
const app = getApp()
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lastTime: 0,
    orderInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      oid: options.oid
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.orderDetails();
  },

  //订单详情
  orderDetails: function() {
    var that = this;
    var oid = this.data.oid;
    api.POST({
      params: {
        "oid": oid
      },
      url: app.globalData.base_url + 'shop/getOrderInfo',
      success: function(res) {
        if (util.isEmpty(res.data.code)) {
          var orderInfo = res.data.data;
          var productList = res.data.data.productList;
          var newArr = [];
          var pickupCode = orderInfo.pickup_code;
          for (var i = 0; i < pickupCode.length; i++) {
            var item = pickupCode.substr(i, 1);
            newArr.push(item)
          }
          var numbers = 0;
          for (var i = 0; i < productList.length; i++) {
            numbers = numbers / 1 + productList[i].number / 1;
          }
          that.setData({
            orderInfo: orderInfo,
            newArr: newArr,
            numbers: numbers
          })
        } else {
          util.showError(res.data.message);
          setTimeout(function() {
            wx.navigateBack({
              delta: 1
            })
          }, 2000);
        }
      },
      fail: function(fail) {
        util.showError("网络错误，请检查网络。");
      }
    })
  },

  //取消提货
  cancelClaimGoods: function(e) {
    var that = this;
    var curTime = e.timeStamp;
    var lastTime = this.data.lastTime;
    this.setData({
      lastTime: curTime
    })
    if ((lastTime > 0) && (curTime - lastTime < 1500)) {
      return false; //禁止连续点击
    }
    wx.showModal({
      title: '提示',
      content: '确定取消提货',
      success: function(res) {
        if (res.confirm) {
          api.POST({
            params: {
              oid: that.data.oid
            },
            url: app.globalData.base_url + 'shop/cancelPickUp',
            success: function(res) {
              if (util.isEmpty(res.data.code)) {
                util.showError("取消提货成功");
                setTimeout(function() {
                  wx.navigateBack({
                    delta: 1
                  })
                }, 2000)
              } else {
                util.showError(res.data.message);
              }
            },
            fail: function(fail) {
              util.showError("网络错误，请检查网络。");
            }
          })
        }
      }
    })


  },

  //打开柜门
  openDoor: function(e) {
    var that = this;
    var curTime = e.timeStamp;
    var lastTime = this.data.lastTime;
    this.setData({
      lastTime: curTime
    })
    if (lastTime > 0) {
      if (curTime - lastTime < 1500) {
        return false; //禁止连续点击
      }
    }
    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {
        var rest = res.result;
        if (rest.indexOf("gzjShopId?machineid") < 0) {
          util.showStatus("请扫描机器二维码");
          return;
        }
        var arr = res.result.split("?");
        var str1 = arr[1];
        var atrr1 = str1.split("&")[0];
        var machine_id = atrr1.split("=")[1];
        that.setData({
          machine_id: machine_id
        })
        wx.navigateTo({
          url: '../openDoor/openDoor?machine_id=' + machine_id + '&oid=' + that.data.oid,
        })
      }
    })
  },

  // 订单支付
  pay: function() {
    var that = this;
    api.POST({
      params: {
        oid: that.data.oid
      },
      url: app.globalData.base_url + 'payment/getWechatInfo',
      success: function(res) {
        if (util.isEmpty(res.data.code)) {
          var preData = res.data.data.payinfo;
          wx.requestPayment({
            timeStamp: preData.timeStamp,
            nonceStr: preData.nonceStr,
            package: preData.package,
            signType: preData.signType,
            paySign: preData.paySign,
            success: function(res) {
              that.onShow();
            },
            fail: function(error) {
              util.showError("支付失败");
            }
          })
        } else {
          util.showError(res.data.message);
        }
      },
      fail: function(fail) {
        util.showError("网络错误，请检查网络。");
      }
    })
  },

  //联系客服
  callServices: function() {
    wx.makePhoneCall({
      phoneNumber: app.globalData.service_phone
    })
  },

  //开发票
  openInvoice: function () {
    wx.navigateTo({
      url: '../invoice/invoice?oid=' + this.data.oid,
    })
  },

  //图片预览
  itemImgClick: function (e) {
    var url = e.currentTarget.dataset.url;
    var that = this;
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: that.data.orderInfo.exchange_img // 需要预览的图片http链接列表
    })
  },
})